{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "5fd5e478",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " Retrying in 1 seconds\n",
      "step 0\n",
      "-1073741824.0\n",
      "step 1\n",
      "-1073741824.0\n",
      "step 2\n",
      "-1073741824.0\n",
      "step 3\n",
      "-1073741824.0\n",
      "step 4\n",
      "5.0\n",
      "step 5\n",
      "6.441688669705763\n",
      "step 6\n",
      "8.115447296504863\n",
      "step 7\n",
      "10.656324048852548\n",
      "step 8\n",
      "12.489382814033888\n",
      "step 9\n",
      "14.123148219916038\n",
      "step 10\n",
      "16.01429042187519\n",
      "step 11\n",
      "18.606444805837235\n",
      "step 12\n",
      "20.600430448865517\n",
      "step 13\n",
      "22.464272133610212\n",
      "step 14\n",
      "24.985937773110347\n",
      "step 15\n",
      "27.167384529672567\n",
      "step 16\n",
      "29.70732797479723\n",
      "step 17\n",
      "31.908145092544153\n",
      "step 18\n",
      "33.28739424322267\n",
      "step 19\n",
      "34.733481557108476\n",
      "step 20\n",
      "35.3323926259391\n",
      "step 21\n",
      "35.99785730851348\n",
      "step 22\n",
      "35.04614726855419\n",
      "step 23\n",
      "34.79643260369543\n",
      "step 24\n",
      "35.14940797859803\n",
      "step 25\n",
      "35.8718116966635\n",
      "step 26\n",
      "35.50525129398797\n",
      "step 27\n",
      "35.69670572132804\n",
      "step 28\n",
      "35.8216625102656\n",
      "step 29\n",
      "35.726194450315084\n",
      "step 30\n",
      "35.924270868077876\n",
      "step 31\n",
      "35.10977242021821\n",
      "step 32\n",
      "35.14090866431128\n",
      "step 33\n",
      "34.79207736683078\n",
      "step 34\n",
      "35.39965660882648\n",
      "step 35\n",
      "35.821081126770004\n",
      "step 36\n",
      "35.801635324149395\n",
      "step 37\n",
      "35.84261809474323\n",
      "step 38\n",
      "35.677505729240366\n",
      "step 39\n",
      "34.8334609557502\n",
      "step 40\n",
      "35.78003523350693\n",
      "step 41\n",
      "34.88148811253253\n",
      "step 42\n",
      "35.89160402168985\n",
      "step 43\n",
      "35.300871023195796\n",
      "step 44\n",
      "35.74202830573544\n",
      "step 45\n",
      "35.53837872798089\n",
      "step 46\n",
      "34.87633715277538\n",
      "step 47\n",
      "35.20435195896774\n",
      "step 48\n",
      "34.91504943322856\n",
      "step 49\n",
      "34.92583393730223\n",
      "step 50\n",
      "34.994780645435675\n",
      "step 51\n",
      "35.844286391846836\n",
      "step 52\n",
      "35.595173021066\n",
      "step 53\n",
      "35.47213689671829\n",
      "step 54\n",
      "35.21454219900537\n",
      "step 55\n",
      "35.995558782955634\n",
      "step 56\n",
      "35.04671493369155\n",
      "step 57\n",
      "35.20418036825489\n",
      "step 58\n",
      "35.56302228556946\n",
      "step 59\n",
      "35.05306087889243\n",
      "step 60\n",
      "35.397500759884714\n",
      "step 61\n",
      "34.77281212544534\n",
      "step 62\n",
      "34.87123023414519\n",
      "step 63\n",
      "35.03205225915648\n",
      "step 64\n",
      "35.48360412785318\n",
      "step 65\n",
      "35.41787598180584\n",
      "step 66\n",
      "35.55567516445648\n",
      "step 67\n"
     ]
    },
    {
     "ename": "TraCIException",
     "evalue": "Could not add subscription. Vehicle 'vehicle_2' is not known.Vehicle 'vehicle_2' is not known.",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTraCIException\u001b[0m                            Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-1-b2c787303c84>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m     12\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mstep\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1000\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     13\u001b[0m     \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"step\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstep\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 14\u001b[0;31m     \u001b[0mtraci\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvehicle\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msubscribe\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvehID\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mtc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mVAR_ROAD_ID\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mVAR_LANEPOSITION\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     15\u001b[0m     \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtraci\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvehicle\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgetSpeed\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvehID\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     16\u001b[0m     \u001b[0mtraci\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msimulationStep\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/lib/python3.8/site-packages/traci/domain.py\u001b[0m in \u001b[0;36msubscribe\u001b[0;34m(self, objectID, varIDs, begin, end, parameters)\u001b[0m\n\u001b[1;32m    186\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mvarIDs\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    187\u001b[0m             \u001b[0mvarIDs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_subscriptionDefault\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 188\u001b[0;31m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_connection\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_subscribe\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_subscribeID\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbegin\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mend\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mobjectID\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvarIDs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mparameters\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    189\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    190\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0munsubscribe\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mobjectID\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/lib/python3.8/site-packages/traci/connection.py\u001b[0m in \u001b[0;36m_subscribe\u001b[0;34m(self, cmdID, begin, end, objID, varIDs, parameters)\u001b[0m\n\u001b[1;32m    288\u001b[0m                     \u001b[0mformat\u001b[0m \u001b[0;34m+=\u001b[0m \u001b[0;34m\"s\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    289\u001b[0m                     \u001b[0margs\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mparameters\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mv\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 290\u001b[0;31m         \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_sendCmd\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcmdID\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mbegin\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mend\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mobjID\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mformat\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    291\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mvarIDs\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    292\u001b[0m             \u001b[0mobjectID\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mresponse\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_readSubscription\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mresult\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/lib/python3.8/site-packages/traci/connection.py\u001b[0m in \u001b[0;36m_sendCmd\u001b[0;34m(self, cmdID, varID, objID, format, *values)\u001b[0m\n\u001b[1;32m    223\u001b[0m             \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_string\u001b[0m \u001b[0;34m+=\u001b[0m \u001b[0mstruct\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpack\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"!i\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mobjID\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mobjID\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mencode\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"latin1\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    224\u001b[0m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_string\u001b[0m \u001b[0;34m+=\u001b[0m \u001b[0mpacked\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 225\u001b[0;31m         \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_sendExact\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    226\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    227\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0m_readSubscription\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mresult\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/lib/python3.8/site-packages/traci/connection.py\u001b[0m in \u001b[0;36m_sendExact\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m    140\u001b[0m                 \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_string\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mbytes\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    141\u001b[0m                 \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_queue\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 142\u001b[0;31m                 \u001b[0;32mraise\u001b[0m \u001b[0mTraCIException\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0merr\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mprefix\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0m_RESULTS\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mprefix\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    143\u001b[0m             \u001b[0;32melif\u001b[0m \u001b[0mprefix\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m!=\u001b[0m \u001b[0mcommand\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    144\u001b[0m                 \u001b[0;32mraise\u001b[0m \u001b[0mFatalTraCIError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Received answer %s for command %s.\"\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mprefix\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcommand\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mTraCIException\u001b[0m: Could not add subscription. Vehicle 'vehicle_2' is not known.Vehicle 'vehicle_2' is not known."
     ]
    }
   ],
   "source": [
    "#接入traci\n",
    "import sys,os,subprocess\n",
    "import traci\n",
    "import traci.constants as tc\n",
    "\n",
    "sumoBinary = \"/usr/bin/sumo\" #sumo or sumo-gui connection\n",
    "sumoCmd = [sumoBinary, \"-c\", \"/home/hhhhh/Desktop/sumo/demo/v1.sumocfg\"]\n",
    "traci.start(sumoCmd)\n",
    "\n",
    "step = 0\n",
    "vehID='vehicle_2'\n",
    "for step in range(1000):\n",
    "    print(\"step\", step)\n",
    "    traci.vehicle.subscribe(vehID, (tc.VAR_ROAD_ID, tc.VAR_LANEPOSITION))\n",
    "    print(traci.vehicle.getSpeed(vehID))\n",
    "    traci.simulationStep()\n",
    "traci.close()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "70ab5b9f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Object `traci.constants` not found.\n"
     ]
    }
   ],
   "source": [
    "??traci.constants"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6a5d0a14",
   "metadata": {},
   "outputs": [],
   "source": [
    "??traci.vehicle.getNeighbors"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "25ab3617",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8792ddee",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
